app.controller('houseOwnerDetailReadCtl',
     function($scope, $http, rest, $compile, $location, ngDialog, tipService, user, $state, $window, uiLoad, $rootScope, $ocLazyLoad, $timeout, uploadConf, uploadprefix) {

          var token = $window.localStorage.getItem("token");

          $scope.uploadprefix = uploadprefix;

          if ($window.localStorage.getItem("houseId")) {
               $scope.houseId = $window.localStorage.getItem("houseId");
          }
          $scope.queryParams = {
               houseIds: $scope.houseId
          }

          $scope.column2 = 0; //分栏
          $scope.maxImgNum = 5; //最大图片数量


          $scope.imgNew = [{
               text: '卧室照片',
               name: 'bedroomPhoto',
               value: [],
               picType: 0,
          }, {
               text: '客厅照片',
               name: 'drawingroomPhoto',
               value: [],
               picType: 0,
          }, {
               text: '卫生间照片',
               name: 'washroomPhoto',
               value: [],
               picType: 0,
          }, {
               text: '厨房照片',
               name: 'kitchenPhoto',
               value: [],
               picType: 0,
          }, {
               text: '房门照片',
               name: 'surroundEnvironment',
               value: [],
               picType: 0,
          }, {
               text: '周边环境',
               name: 'roomDoorPhoto',
               value: [],
               picType: 0,
          }, {
               text: '小区门口照',
               name: 'villageDoorPhoto',
               value: [],
               picType: 0,
          }, {
               text: '楼栋号照',
               name: 'bulidNumberPhoto',
               value: [],
               picType: 0,
          }, {
               text: '大门房间照片',
               name: 'roomNumberPhoto',
               value: [],
               picType: 0,
          }, {
               text: '产权相关文件',
               name: 'stateownedPropertyFile',
               value: [],
               picType: 0,
          }, {
               text: '转租签约整套合同',
               name: 'changeLeaseContractPhoto',
               value: [],
               picType: 0,
          }];
          uploadConf.column2 = $scope.column2;
          uploadConf.maxImgNum = $scope.maxImgNum;
          uploadConf.imgNew = $scope.imgNew;

          applyHouseDetail();

          function applyHouseDetail() {
               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/detail",
                    data: {
                         houseId: $scope.houseId,
                    },
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == "0000") {
                              $scope.$apply(function() {
                                   $scope.queryParams = data.result;
                              })

                              if ($scope.queryParams.isRentHouse == "true") {
                                   $scope.isRentHouse = "是";
                              } else {
                                   $scope.isRentHouse = "否";
                              }

                              if ($scope.queryParams.isDecorateApply == "true") {
                                   $scope.isDecorateApply = "是";
                              } else {
                                   $scope.isDecorateApply = "否";
                              }

                              if ($scope.queryParams.rentStatus == "true") {
                                   $scope.rentStatus = "有客";
                              } else {
                                   $scope.rentStatus = "无客";
                              }

                              //判断是否是指派过的房源
                              if ($scope.queryParams.assignedUserId == null) {
                                   $scope.assigned = 0;
                              } else {
                                   $scope.assigned = 1;
                                   if ($scope.queryParams.houseProcessStatus == "HOUSEFIRSTTRIALING") {
                                        $scope.firstExm = 1;
                                   } else if ($scope.queryParams.houseProcessStatus == "HOUSEDECORATEASSESSMENTING") {
                                        $scope.decorate = 1;
                                   } else if ($scope.queryParams.houseProcessStatus == "HOUSEJUDGMENTING") {
                                        $scope.firstExm = 1;
                                   }
                              }

                              if ($scope.queryParams.houseProcessStatus == "HOUSEFIRSTTRIAL" || $scope.queryParams.houseProcessStatus == "HOUSEDECORATEASSESSMENT" || $scope.queryParams.houseProcessStatus == "HOUSEJUDGMENT") {
                                   $('#suggest').attr('readonly', 'readonly');
                                   $('#totalDecorationMoney').attr('readonly', 'readonly');
                                   $('#practicalDecorationMoney').attr('readonly', 'readonly');
                                   $('#judgmentMoney').attr('readonly', 'readonly');
                                   $('#judgmentRepetitionMoney').attr('readonly', 'readonly');
                              } else {

                              }

                              if ($scope.queryParams.houseProcessStatus == "HOUSEDECORATEASSESSMENT" || $scope.queryParams.houseProcessStatus == "HOUSEDECORATEASSESSMENTING" || $scope.queryParams.houseProcessStatus == "HOUSEDECORATEASSESSMENTSUCCEED") {
                                   $scope.decoratePrice = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "HOUSEJUDGMENT" || $scope.queryParams.houseProcessStatus == "HOUSEJUDGMENTING") {
                                   $scope.decoratePrice = 1;
                                   $scope.finalPrice = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "HOUSEJUDGMENTREPETITION" || $scope.queryParams.houseProcessStatus == "HOUSEJUDGMENTREPETITIONSUCCEED") {
                                   $scope.decoratePrice = 1;
                                   $scope.finalPrice = 1;
                                   $scope.seniorPrice = 1;
                              }

                              if ($scope.queryParams.houseProcessStatus == "HouseFirstTrial") {
                                   $scope.statusStr = "待初审";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseFirstTrialing") {
                                   $scope.statusStr = "初审中";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseFirstTrialSuccess") {
                                   $scope.statusStr = "初审通过";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseBack") {
                                   $scope.statusStr = "初审拒绝";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseDecorateAssessment") {
                                   $scope.statusStr = "待装修评估";
                                   $scope.decoratePrice = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "HouseDecorateAssessmenting") {
                                   $scope.statusStr = "装修评估中";
                                   $scope.decoratePrice = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "HouseDecorateAssessmentSuccess") {
                                   $scope.statusStr = "装修评估通过";
                                   $scope.decoratePrice = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgment") {
                                   $scope.statusStr = "待终审";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgmenting") {
                                   $scope.statusStr = "终审中";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgmentSuccess") {
                                   $scope.statusStr = "终审通过";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgmentBack") {
                                   $scope.statusStr = "终审拒绝";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgmentRepetition") {
                                   $scope.statusStr = "待高管审核";
                                   $scope.assigned = 1;
                                   $scope.seniorManager = 1;
                              } else if ($scope.queryParams.houseProcessStatus == "AccountSuccessful") {
                                   $scope.statusStr = "高管审核通过";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseJudgmentRepetitionBack") {
                                   $scope.statusStr = "高管审核拒绝";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseSupplement") {
                                   $scope.statusStr = "补件";
                              } else if ($scope.queryParams.houseProcessStatus == "HouseClaim") {
                                   $scope.statusStr = "认领";
                              } else if ($scope.queryParams.houseProcessStatus == "HOUSEASSIGN") {
                                   $scope.statusStr = "指派";
                              }

                              var pushPhoto = function(photoString) {
                                   var photoVals = [];
                                   if (photoString && photoString.length > 0) {
                                        photoVals = photoString.split(",");
                                   }
                                   return photoVals;
                              }

                              $scope.imgNew[0].value = pushPhoto($scope.queryParams.bedroomPhoto);
                              $scope.imgNew[1].value = pushPhoto($scope.queryParams.drawingroomPhoto);
                              $scope.imgNew[2].value = pushPhoto($scope.queryParams.washroomPhoto);
                              $scope.imgNew[3].value = pushPhoto($scope.queryParams.kitchenPhoto);
                              $scope.imgNew[4].value = pushPhoto($scope.queryParams.roomDoorPhoto);
                              $scope.imgNew[5].value = pushPhoto($scope.queryParams.surroundEnvironment);
                              $scope.imgNew[6].value = pushPhoto($scope.queryParams.villageDoorPhoto);
                              $scope.imgNew[7].value = pushPhoto($scope.queryParams.bulidNumberPhoto);
                              $scope.imgNew[8].value = pushPhoto($scope.queryParams.roomNumberPhoto);
                              $scope.imgNew[9].value = pushPhoto($scope.queryParams.stateownedPropertyFile);
                              $scope.imgNew[10].value = pushPhoto($scope.queryParams.changeLeaseContractPhoto);

                              $scope.$apply();

                              uploadConf.imgNew = $scope.imgNew;
                              console.log(uploadConf);

                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                         }

                    }
               });
          }

          $("#suggest").on("input propertychange", function() {
               var $this = $(this),
                    _val = $this.val(),
                    count = "";
               if (_val.length > 200) {
                    $this.val(_val.substring(0, 200));
               }
               count = 200 - $this.val().length;
               $("#text-count").text(count);
          });

          $scope.viewRecord = function() {
               ngDialog.openConfirm({
                    template: 'views/houseOwner/viewRecord.html',
                    resolve: {
                         load: function() {
                              return $ocLazyLoad.load('angularFileUpload').then(
                                   function() {
                                        return uiLoad.load(['js/controllers/houseOwner/viewRecord.js'])
                                   });
                         }
                    },
                    data: {
                         id: $scope.queryParams.id,
                    },
                    scope: $scope,
                    width: 900,
                    // height: 700
               }).then(function(data) {

               }, function(reason) {

               });
          }

          //认领
          $scope.claimHouse = function() {
               ngDialog.openConfirm({
                    template: 'js/module/fund/claimHouse.html',
                    resolve: {
                         load: function() {
                              return $ocLazyLoad.load('angularFileUpload').then(
                                   function() {
                                        return uiLoad.load(['js/module/fund/claimHouse.js'])
                                   });
                         }
                    },
                    data: {
                         houseId: $scope.houseId
                    },
                    scope: $scope,
                    width: 600,
                    // height: 700
               }).then(function(data) {

               }, function(reason) {
                    applyHouseDetail();
               });
          }

          // $scope.assginHouseTo = function() {
          //      ngDialog.openConfirm({
          //           template: 'js/module/fund/assignTo.html',
          //           resolve: {
          //                load: function() {
          //                     return uiLoad.load(['js/module/fund/assignTo.js'])
          //                }
          //           },
          //           data: {
          //                houseId: $scope.houseId
          //           },
          //           scope: $scope,
          //           width: 600,
          //           // height: 700
          //      }).then(function(data) {

          //      }, function(reason) {
          //           applyHouseDetail();
          //      });
          // }

          //保存事件
          $scope.saveHouseDetail = function() {

               var updataPhoto = function(photoStr) {
                    // return photoStr.join(",");

                    var photoVals = "";
                    for (var i = 0; i < photoStr.length; i++) {
                         if (photoStr.length == 1) {
                              photoVals1 = photoStr[0]
                         } else {
                              photoVals += photoStr[i] + ",";
                              var photoVals1 = photoVals.substring(0, photoVals.length - 1);
                         }
                    }
                    return photoVals1;
               }

               $scope.queryParams.bedroomPhoto = updataPhoto($scope.imgNew[0].value);
               $scope.queryParams.drawingroomPhoto = updataPhoto($scope.imgNew[1].value);
               $scope.queryParams.washroomPhoto = updataPhoto($scope.imgNew[2].value);
               $scope.queryParams.kitchenPhoto = updataPhoto($scope.imgNew[3].value);
               $scope.queryParams.surroundEnvironment = updataPhoto($scope.imgNew[4].value);
               $scope.queryParams.roomDoorPhoto = updataPhoto($scope.imgNew[5].value);
               $scope.queryParams.villageDoorPhoto = updataPhoto($scope.imgNew[6].value);
               $scope.queryParams.bulidNumberPhoto = updataPhoto($scope.imgNew[7].value);
               $scope.queryParams.roomNumberPhoto = updataPhoto($scope.imgNew[8].value);
               $scope.queryParams.stateownedPropertyFile = updataPhoto($scope.imgNew[9].value);
               $scope.queryParams.changeLeaseContractPhoto = updataPhoto($scope.imgNew[10].value);

               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/updateHouse",
                    data: $scope.queryParams,
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == 0000) {
                              tipService.show("body", {
                                   message: "保存成功！",
                              }, 3);
                              history.go(-1);
                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                              return;
                         }
                    }
               });
          }

          //回退补件
          $scope.fallback = function() {
               $scope.queryParams.houseIds = $scope.houseId;
               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/houseSupplementProcess",
                    data: $scope.queryParams,
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == 0000) {
                              tipService.show("body", {
                                   message: data.message,
                              }, 3);
                              $state.go('app.house.houseManageList');
                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                              return;
                         }
                    }
               });
          }

          //通过
          $scope.passThrough = function() {
               $scope.queryParams.houseIds = $scope.houseId;
               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/updateHouseProcess",
                    data: $scope.queryParams,
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == 0000) {
                              tipService.show("body", {
                                   message: data.message,
                              }, 3);
                              // $state.go('app.house.houseManageList');
                              history.go(-1);
                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                              return;
                         }
                    }
               });
          }

          //拒绝
          $scope.refuse = function() {
               $scope.queryParams.houseIds = $scope.houseId;
               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/updateHouseRepulse",
                    data: $scope.queryParams,
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == 0000) {
                              tipService.show("body", {
                                   message: data.message,
                              }, 3);
                              // $state.go('app.house.houseManageList');
                              history.go(-1);
                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                              return;
                         }
                    }
               });
          }

          //高管通过
          $scope.seniorPass = function() {
               $scope.queryParams.houseIds = $scope.houseId;
               rest.decoJava({
                    type: "post",
                    url: "/tenancy-sys/admin/house/updateHouseProcess",
                    data: $scope.queryParams,
                    beforeSend: function(request) {
                         request.setRequestHeader("token", token);
                    },
                    success: function(data) {
                         if (data.code == 0000) {
                              tipService.show("body", {
                                   message: data.message,
                              }, 3);
                              // $state.go('app.house.houseManageList');
                              history.go(-1);
                         } else {
                              tipService.show("body", {
                                   message: data.message,
                                   notice: "danger"
                              }, 3);
                              return;
                         }
                    }
               });
          }

          $('.exit').on('click', function() {
               window.history.go(-1);
          })


     });